package org.lq.ht.view.vip;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.lq.ht.entity.Vip;
import org.lq.ht.service.DictionaryService;
import org.lq.ht.service.VipService;
import org.lq.ht.service.impl.DictionaryServiceImpl;
import org.lq.ht.service.impl.VipServiceImpl;
import org.lq.ht.util.CastUtil;


import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;

public class V_VipMain extends JFrame {

	private JPanel contentPane;
	private static JTable table;
	private JTextField textField;

	public JPanel getPanel() {
		return contentPane;
	}

	private  int vid;

	public int  getVid() {
		return vid;
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_VipMain frame =new  V_VipMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public V_VipMain() {
		setTitle("会员展示");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 733, 426);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel, BorderLayout.NORTH);

		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//添加
				V_VipAdd add = new V_VipAdd();
				add.setBounds(V_VipMain.this.getX()+50, V_VipMain.this.getY()+50, add.getWidth(), add.getHeight());
				add.setVisible(true);
				add.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						// TODO Auto-generated method stub
						initTable();
					}
				});
			}
		});
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//修改

				int index = table.getSelectedRow();
				if(index>=0) {
					vid = CastUtil.castInt(table.getValueAt(index, 0));
					InitPassword password1 = new InitPassword(vid);
					password1.setBounds(V_VipMain.this.getX()+200, V_VipMain.this.getY()+200, password1.getWidth(), password1.getHeight());
					password1.setVisible(true);
					password1.addWindowListener(new WindowAdapter() {
						@Override
						public void windowClosed(WindowEvent e) {
							// TODO Auto-generated method stub
							V_VipAdd add = password1.getAdd();
							if(add==null) {
								System.out.println("未选择！为输入密码");
							}else {
								add.addWindowListener(new WindowAdapter() {
									@Override
									public void windowClosed(WindowEvent e) {
										// TODO Auto-generated method stub
										initTable();
										System.out.println("=======");
									}
								});
							}
						}
					});


				}else {
					JOptionPane.showMessageDialog(V_VipMain.this, "请先选择VIP！");
				}
			}
		});
		panel.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//删除
				int index = table.getSelectedRow();
				//				System.out.println(index);
				if(index>=0) {
					//卡号删除
					int vid = CastUtil.castInt(table.getValueAt(index, 0));
					VipService vs = new VipServiceImpl();
					if(vs.deleteVipByVid(vid)) {
						JOptionPane.showMessageDialog(V_VipMain.this, "删除成功！");
						initTable();
					}
					else {
						JOptionPane.showMessageDialog(V_VipMain.this, "删除失败！");
					}
				}else {
					JOptionPane.showMessageDialog(V_VipMain.this, "请先选择VIP！");
				}
			}
		});
		panel.add(btnNewButton_2);

		JButton btnNewButton_3 = new JButton("级别设置");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//会员类型
				V_VipType leave= new V_VipType();
				leave.setBounds(V_VipMain.this.getX()+50, V_VipMain.this.getY()+50, leave.getWidth(), leave.getHeight());
				leave.setVisible(true);
			}
		});
		panel.add(btnNewButton_3);

		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"所有", "卡号", "用户名", "级别"}));
		panel.add(comboBox);

		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				//按的回车
				if(e.getKeyCode()==10) {
					//查询
					String type =  comboBox.getSelectedItem().toString();
					String value = textField.getText();
					VipService vs = new VipServiceImpl();
					List<Vip> list = vs.findVipByValue(type, value);
					if(list!=null) {
						initTable(list);
						textField.setText("");
					}
				}
			}
		});
		panel.add(textField);


		JButton btnNewButton_4 = new JButton("查询");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String type =  comboBox.getSelectedItem().toString();
				String value = textField.getText();
				VipService vs = new VipServiceImpl();
				List<Vip> list = vs.findVipByValue(type, value);
				if(list!=null) {
					initTable(list);
					textField.setText("");
				}
			}
		});
		panel.add(btnNewButton_4);
		textField.setColumns(10);

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
//				System.out.println(e.getClickCount());
				if(e.getClickCount()==2) {
					int index = table.getSelectedRow();
					vid = (int) table.getValueAt(index, 0);
				}
			}
		});

		table.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null, null, null, null, null, null, null},
				},
				new String[] {
						"\u5361\u53F7", "\u540D\u5B57", "\u8BC1\u4EF6\u53F7", "\u4F1A\u5458\u7EA7\u522B", "\u6298\u6263", "\u79EF\u5206", "\u7535\u8BDD\u53F7", "\u529E\u5361\u65E5\u671F", "\u5730\u5740", "\u5907\u6CE8"
				})
		
//		可以双击双击
//		对new DefaultTableModel() 中的方法进行了重写
		{
			boolean[] columnEditables = new boolean[] {
					true, false, false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane.setViewportView(table);
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);

		JMenuItem mntmNewMenuItem = new JMenuItem("选择");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//选择
				int index = table.getSelectedRow();
				vid = (int) table.getValueAt(index, 0);
				dispose();
			}
		});
		popupMenu.add(mntmNewMenuItem);

		initTable();
		}

		public void initTable(List<Vip> list) {

			VipService vs = new VipServiceImpl();
			DictionaryService ds = new DictionaryServiceImpl();

			DefaultTableModel dtm = (DefaultTableModel) table.getModel();

			int count = dtm.getRowCount();
			for (int i = 0; i < count; i++) {
				dtm.removeRow(0);
			}

			for (Vip v : list) {

				dtm.addRow(new  Object[] {
						v.getVid(),
						v.getName(),
						v.getId_card(),
						ds.getById(v.getType()).getName(),
						v.getDiscount(),
						v.getIntegral(),
						v.getPhone(),
						v.getHandlecarddate(),
						v.getAddress(),
						v.getNote()
				});
			}
		}

		public  void initTable() {

			System.out.println("--++++");
			VipService vs = new VipServiceImpl();
			List<Vip> list = vs.findVipAll();
			DictionaryService ds = new DictionaryServiceImpl();

			DefaultTableModel dtm = (DefaultTableModel) table.getModel();

			int count = dtm.getRowCount();
			for (int i = 0; i < count; i++) {
				dtm.removeRow(0);
			}

			for (Vip v : list) {
				dtm.addRow(new  Object[] {
						v.getVid(),
						v.getName(),
						v.getId_card(),
						ds.getById(v.getType()).getName(),
						v.getDiscount(),
						v.getIntegral(),
						v.getPhone(),
						v.getHandlecarddate(),
						v.getAddress(),
						v.getNote()
				});
			}

		}

		private static void addPopup(Component component, final JPopupMenu popup) {
			component.addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e) {
					if (e.isPopupTrigger()) {
						showMenu(e);
					}
				}
				public void mouseReleased(MouseEvent e) {
					if (e.isPopupTrigger()) {
						showMenu(e);
					}
				}
				private void showMenu(MouseEvent e) {
					int index= table.rowAtPoint(e.getPoint());
					table.setRowSelectionInterval(index, index);
					popup.show(e.getComponent(), e.getX(), e.getY());
				}
			});
		}
	}
